﻿@{
    ViewBag.Title = "事项管理";
    Layout = "~/Views/Shared/_indexBase3.cshtml";  //标准子模块首页模板
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery-2.0.3.js"></script>
        <script src="~/LiberUI/Scripts/jquery.ba-resize.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Controllers.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
        <script src="~/SiteUI/BaseScripts/site.js"></script>
    }
    <link href="~/SiteUI/Issue/Issue.css" rel="stylesheet" />
}

@section LeftNavi{
    <ul id="index_StatusNavi">
        <li id="index_AllStatus" status="all">-全部事项<span>-</span></li>
        <li id="index_ToDiscus" status="Pendding" title="还没有确定后继处理方案的事项">-待讨论<span>-</span></li>
        <li id="index_WorkOn" status="Active">-处理中<span>-</span></li>
        <li id="index_Aborted" status="Aborted">-中止<span>-</span></li>
        <li id="index_End" status="Completed">-已完成<span>-</span></li>
    </ul>
}

@section HeadCnt{
    <div class="MiddleWraper">
        <h2 class="index_Title">@ViewBag.Title</h2>
        <h3>登记、讨论、跟进项目实施过程中各方面的待办事宜</h3>
    </div>
}

@section HeadBar{
    <div class="LeftWraper">
        <div id="index_slcProj" class="Selector">(请选择项目)</div>
    </div>
    <div class="MiddleWraper">
        <div id="index_ListTools">
            <div id="index_Add" class="index_ToolsItem">登记事项</div>
            <div id="index_SearchBox">
                <input type="search" placeholder="别用，还没做好" /><div id="index_SearchBtn">搜索</div>
            </div>
        </div>
    </div>
}

<div id="index_lstIssueViewsHost">
</div>

@section PageHelper{
    
}

@*临时容器，放置需动态展现的自定义页面部件*@
<div id="tmp">
    @RenderPage("/SiteUI/wgBase.cshtml")
    @RenderPage("/SiteUI/Issue/wgIssue.cshtml")
</div>

<script type="text/javascript">
    Env.User = {
        Name: '@ViewBag.UserName',
        ID: '@ViewBag.Uid'
    };
    Env.MyProjs = [];
    Env.Where = {
        Status: 'all',
        ProjId: '-1',
        Page: 0,
        Limit:50
    }

    ///点击新增按钮时调用新建窗体，并设置初始值
    function ShowNewWnd() {
        var newIssue = {
            Content:"",
            Proj: Env.CProj,
            Rating: 1,
            Poster: Env.User,
            Status:'Pendding'
        }
        wgIssue.frmNewIssue.val(newIssue);
        wgIssue.WndNewIssue.show();
    };

    function SubmitIssue() {
        var r = wgIssue.Checker.Check();
        if (r === true) {
            wgIssue.Checker.Disable();
            var v = wgIssue.frmNewIssue.val();

            wgIssue.WndNewIssue.disable(Env.aniSaving.clone().show());
            $.post("/Issue/New", v, onSubmitEnd, 'json');
        }
        else {
            wgIssue.Checker.FirstErr().focus();
        }
    };

    function onSubmitEnd(rtn) {
        wgIssue.WndNewIssue.enable();
        if (rtn.IsPositive) {
            wgIssue.WndNewIssue.hide();
            wgIssue.lstIssueViews.Prepend(rtn.Data);
            Env.mnuUserProjs.Viewer($("#index_slcProj"), setCProj); //设置项目待选列表控件所绑定的视图目标
            GetStatusNavi();

            //TODO:保存历史提交人
            if (Env.CommonProposers == null) {
                Env.CommonProposers = [];
            }
            for (var i = 0; i < Env.CommonProposers.length; i++) {
                if (Env.CommonProposers[i] == rtn.Data.Proposer) {
                    return;
                }
            }
            Env.CommonProposers.push(rtn.Data.Proposer);
            Env.mnuProposers.Menu().Append(rtn.Data.Proposer);
        }
        else {
            console.log('提交失败怎么办？');
        }
    };

    function onWndHide() {
        Env.mnuUserProjs.Viewer($("#index_slcProj"), setCProj);
    };
    
    //处理按进度状态导航
    function onNaviByStatus(ev) {
        var $elm = $(ev.target);
        var s = $elm.attr('Status');
        if (s) {
            Env.Where.Status = $elm.attr("Status");
            wgIssue.lstIssueViews.Clear();
            Env.aniLoading.appendTo($('#index_lstIssueViewsHost')).show();
            new AjaxHelper("get", "/Issue/Query", Env.Where, RefreshLister);
            $("#index_StatusNavi>li").removeClass("actived");
            $elm.addClass("actived");
        }
    };

    function GetStatusNavi() {
        new AjaxHelper("getJSON", "/Issue/CountingByStatus", Env.Where, RefreshStatusMenu);
    };

    function RefreshStatusMenu(rtn) {
        if (rtn.IsPositive) {
            var m = $("#index_StatusNavi");
            var sb = m.children("#index_ToDiscus").children("span");
            var wo = m.children("#index_WorkOn").children("span");
            var ab = m.children("#index_Aborted").children("span");
            var en = m.children("#index_End").children("span");

            rtn.Data.Pendding ? sb.html(rtn.Data.Pendding) : sb.html('0');
            rtn.Data.Active ? wo.html(rtn.Data.Active) : wo.html('0');
            rtn.Data.Aborted ? ab.html(rtn.Data.Aborted) : ab.html('0');
            rtn.Data.Completed ? en.html(rtn.Data.Completed) : en.html('0');
            m.children("#index_AllStatus").children("span").html(rtn.Data.All);
        }
    };

    //设置当前选中的项目
    function setCProj(viewer, ev) {
        Env.CProj = ev.View.val();
        $.Cookie('lastProj', Env.CProj);
        $.Cookie('lastProjId', Env.CProj.IDStr, {expires:30*24*60}); //因后台无法和方便地解析json格式，故需要直接保存ID作为下一次打开时的默认查询条件
        viewer.html(Env.CProj.Name);
        NaviByProj();
    };

    //处理按项目过滤
    function NaviByProj() {
        Env.Where.ProjId = Env.CProj.IDStr;
        wgIssue.lstIssueViews.Clear();
        Env.aniLoading.appendTo($('#index_lstIssueViewsHost')).show();
        new AjaxHelper("getJSON", "/Issue/Query", Env.Where, RefreshLister);
    };

    //刷新事务项列表
    function RefreshLister(rtn) {
        Env.aniLoading.hide();
        wgIssue.lstIssueViews.Clear();
        if (rtn.IsPositive) {
            var tmp = [];
            for (var i = 0, n = rtn.Data.length; i < n; i++) {
                tmp.push(rtn.Data[i]);
            }
            wgIssue.lstIssueViews.Prepend(tmp);
        }
        GetStatusNavi();
    };

    $(function () {
        //创建查询部件
        Env.btnSeach = new Button($("#index_SearchBtn"));

        //设置项目过滤控件
        Env.MyProjs = '@ViewBag.MyProjects'.ToJSON();

        if (Env.mnuUserProjs) {
            Env.mnuUserProjs.Menu().Append(Env.MyProjs);
            Env.mnuUserProjs.Viewer($("#index_slcProj"), setCProj);
        }
        if (Env.MyProjs&&Env.MyProjs.length > 0) {
            Env.Where.ProjId = Env.MyProjs[0].IDStr;
        }

        //设置新增按钮
        Env.btnNew = $("#index_Add").on('click', ShowNewWnd);

        //设置列表控件并添加初始数据
        wgIssue.lstIssueViews.dom($("#index_lstIssueViewsHost"));

        //由于初始数据不是用jQuery的getJSON方法，需要手动进行JSON解析
        var dd = "@ViewBag.Datas".ToJSON();
        if (!dd.IsPositive||dd.Data.length===0){
            
        }
        else {
            wgIssue.lstIssueViews.Prepend(dd.Data);
        }
        

        //设置左侧快速导航栏
        $("#index_StatusNavi").click(onNaviByStatus);

        //检查Cookie获取上次选择的项目
        var lp = $.Cookie('lastProjId');
        if (lp&&Env.MyProjs) {
            for (var i = 0; i < Env.MyProjs.length; i++) {
                if (lp == Env.MyProjs[i].IDStr) {
                    Env.CProj = Env.MyProjs[i];
                    Env.Where.ProjId = Env.CProj.IDStr
                    Env.mnuUserProjs.Viewer().html(Env.CProj.Name);
                    break;
                }
            }
        }

        //获取各种不同状态事务项的统计值
        GetStatusNavi();
        Env.CommonProposers = '@ViewBag.CommonProposers'.ToJSON();
        Env.mnuProposers.Menu().Append(Env.CommonProposers);

        wgIssue.WndNewIssue.BtnEnter('提交', SubmitIssue);
        wgIssue.WndNewIssue.on("hide", onWndHide);
    })
</script>
